package com.mall.b2c.plugin.search;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import com.mall.b2c.domain.goods.GoodsCategoryDomain;
import com.mall.common.hibernate.Finder;
import com.mall.common.plugin.AutoRegisterPlugin;
import com.mall.common.util.UrlUtils;
/**
 * 商品剂型筛选器
 * @author wangzx
 */
public class DrugformSearchFilter {//extends AutoRegisterPlugin implements IGoodsSearchFilter{

/*	@Override
	public void filter(Finder finder, GoodsCategoryDomain cat, String urlFragment) {
		if(StringUtils.isNotBlank(urlFragment)) {
			try {
				String drugForm = URLDecoder.decode(urlFragment, "utf-8");
				finder.append( " and g.drugForm = :drugForm");
				finder.setParam("drugForm", drugForm);
			} catch (UnsupportedEncodingException e) {
			}
		}
	}

	@Override
	public List<SearchSelector> createSelectorList(GoodsCategoryDomain cat,
			String url, String urlFragment, Map<String, String> paramMap,
			String hql, Finder finder) {
		List<SearchSelector> searchSelectors = new ArrayList<SearchSelector>();
		*//**已选择剂型*//*
		if(StringUtils.isNotBlank(urlFragment)) {
			try {
				String drugForm = URLDecoder.decode(urlFragment, "utf-8");
				SearchSelector searchSelector = new SearchSelector();
				searchSelector.setName(drugForm);
				searchSelector.setSelected(Boolean.TRUE);
				searchSelector.setUrl(url);
				searchSelectors.add(searchSelector);
				return searchSelectors;
			} catch (UnsupportedEncodingException e) {
			}
		}
		finder.resetHQL("select g.drugForm, count(*) from " + hql + " group by g.drugForm order by count(*) desc");
		List<Object[]> resultList = finder.findHQLList();
		if(!CollectionUtils.isEmpty(resultList)) {
			for(Object[] rs : resultList) {
				String drugForm = rs[0] != null ? (String)rs[0] : "";
				if(StringUtils.isNotBlank(drugForm)) {
					SearchSelector searchSelector = new SearchSelector();
					searchSelector.setName(drugForm);
					searchSelector.setCount((Long)rs[1]);
					searchSelector.setUrl(UrlUtils.addUrlParam(url, getFilterId(), drugForm, false));
					searchSelector.setSelected(Boolean.FALSE);
					searchSelectors.add(searchSelector);
				}
			}
		}
		return searchSelectors;
	}

	@Override
	public String getFilterId() {
		return "drugform";
	}

	@Override
	public String getSysId() {
		return "drugformList";
	}

	@Override
	public String getType() {
		return "goodsSearchFilter";
	}

	@Override
	public String getId() {
		return "drugformSearchFilter";
	}

	@Override
	public String getName() {
		return "商品剂型筛选器";
	}

	@Override
	public String getVersion() {
		return "1.0";
	}

	@Override
	public String getAuthor() {
		return "wangzx";
	}

	@Override
	public void perform(Object... params) {
		
	}

	@Override
	public void register() {
		
	}*/
}
